﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using Peter.Sdw.Data;

namespace Peter.Sdw.Logic
{
   /// <summary>
   /// View model for a footer.
   /// </summary>
   public class FooterListModel : BaseModel
   {
      private int m_EndIndex;
      private int m_StartIndex;
      private FooterModel m_SelectedFooter;
      private readonly ObservableCollection <FooterModel> m_Footers;

      /// <summary>
      /// Initializes a new footer.
      /// </summary>
      public FooterListModel ()
         : base (null)
      {
         this.m_Footers = new ObservableCollection <FooterModel> ();
      }

      /// <summary>
      /// Gets or Sets if this list has multiple footers, or only one.
      /// </summary>
      public bool HasMultiple { get; set; }

      /// <summary>
      /// Gets or Sets the currently selected start index.
      /// </summary>
      public int StartIndex
      {
         get { return this.m_StartIndex; }
         set
         {
            this.m_StartIndex = value;
            this.RaisePropertyChanged ("StartIndex");
         }
      }

      /// <summary>
      /// Gets or Sets the currently selected end index.
      /// </summary>
      public int EndIndex
      {
         get { return this.m_EndIndex; }
         set
         {
            this.m_EndIndex = value;
            this.RaisePropertyChanged ("EndIndex");
         }
      }

      /// <summary>
      /// Gets the currently selected footer.
      /// </summary>
      public FooterModel SelectedFooter
      {
         get { return this.m_SelectedFooter; }
         set
         {
            this.m_SelectedFooter = value;
            this.RaisePropertyChanged("SelectedFooter");
         }
      }

      /// <summary>
      /// Gets the parent id.
      /// </summary>
      public int ParentId { get; private set; }

      /// <summary>
      /// Gets the item type.
      /// </summary>
      public ItemType ItemType { get; private set; }

      /// <summary>
      /// Gets the list of footers.
      /// </summary>
      public ObservableCollection<FooterModel> Footers { get { return this.m_Footers; } }

      /// <summary>
      /// Gets the footer data from the server.
      /// </summary>
      public void Get (ItemType type, int parentId)
      {
         this.OkToUpdate = false;
         this.IsLoading = true;
         this.ItemType = type;
         this.ParentId = parentId;
         this.Footers.Clear ();
         WebQueue.Instance.WebGetComplete += this.OnGetCompleted;
         WebQueue.Instance.Get (string.Format (Constants.URL_GET_FOOTER, this.ItemType, this.ParentId));
      }

      /// <summary>
      /// Adds the given styles to the current list.
      /// </summary>
      /// <param name="response"></param>
      private void AddFooters (dynamic response)
      {
         if (response.footers != null) {
            foreach (var f in response.footers) {
               var footer = new FooterModel (this) { Text = f.text };
               if (f.styles != null) {
                  foreach (var style in f.styles) {
                     footer.StyleInput.Styles.Add (new Style {
                        Id = style.id,
                        Start = style.start,
                        End = style.end,
                        StartIndex = style.startindex,
                        EndIndex = style.endindex,
                        SpansMultiple = style.multispan
                     });
                  }
               }
               if (f.links != null) {
                  foreach (var link in f.links) {
                     footer.LinkInput.Links.Add (new Link {
                        Id = link.id,
                        Url = link.url,
                        EndIndex = link.endindex,
                        NewWindow = link.newwindow,
                        StartIndex = link.startindex
                     });
                  }
               }
            }
         }
         if (this.Footers.Count == 0) {
            this.Footers.Add( new FooterModel (this));
         }
         this.SelectedFooter = this.Footers.First ();
      }

      /// <summary>
      /// Occurs when a web request is completed.
      /// </summary>
      /// <param name="sender">WebQueue.</param>
      /// <param name="e">WebRequestEventArgs</param>
      private void OnGetCompleted (object sender, WebRequestEventArgs e)
      {
         try {
            if (e.Url == string.Format (Constants.URL_GET_FOOTER, this.ItemType, this.ParentId)) {
               WebQueue.Instance.WebGetComplete -= this.OnGetCompleted;
               if (e.Response.status == Constants.SUCCESS) {
                  this.AddFooters (e.Response);
               }
               this.IsLoading = false;
               this.OkToUpdate = true;
            }
         } catch (Exception ex) {
            Console.Error.WriteLine (ex.Message);
         }
      }
   }
}
